package service

import (
	"blog/dao"
	"blog/models"
	util "blog/utils"
	"errors"
	"log"
)

func Login(username, passwd string) (*models.LoginResponse, error) {
	passwd = util.Md5Crypt(passwd, "mszlu")
	user, err := dao.GetUser(username, passwd)
	if err != nil {
		log.Println("登录失败，账号或者密码错误")
		return nil, errors.New("登录失败，账号或者密码错误")
	}
	token, errAward := util.Award(&user.Uid)
	if errAward != nil {
		return nil, errAward
	}
	return &models.LoginResponse{
		Token: token,
		UserInfo: models.UserInfo{
			Uid:      user.Uid,
			UserName: user.UserName,
			Avator:   user.Avator,
		},
	}, nil
}
